package com.liu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;

/**
 * @ClassName ListValueConstraintValidator
 * @Description
 * @Author LiuXing
 * @Date 2024/3/17 22:01
 * @Version 1.0
 **/
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {
    private HashSet<Integer> set = new HashSet<>();
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] value = constraintAnnotation.value();
        for (int val: value){
            set.add(val);
        }
    }

    /**
     *
     * @param integer   需要校验的数据
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        if (set.contains(integer))
            return true;
        return false;
    }
}
